package com.zlict.commonelasticsearch.controller;

import com.zlict.commonelasticsearch.service.ElasticsearchILMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Elasticsearch ILM策略管理控制器
 * 
 * @author zlict
 */
@RestController
@RequestMapping("/elasticsearch/ilm")
public class ElasticsearchILMController {
    
    @Autowired
    private ElasticsearchILMService elasticsearchILMService;
    
    /**
     * 创建或更新ILM策略
     * 
     * @param retentionMonths 保留月数
     * @return 操作结果
     */
    @PostMapping("/policy")
    public String createOrUpdatePolicy(@RequestParam(defaultValue = "6") int retentionMonths) {
        try {
            // 构建策略名称
            String policyName = "audit-log-policy-" + retentionMonths + "m";
            
            // 创建ILM策略
            boolean success = elasticsearchILMService.createILMPolicy(policyName, retentionMonths);
            
            if (success) {
                return "ILM策略创建/更新成功，保留月数: " + retentionMonths;
            } else {
                return "ILM策略创建/更新失败";
            }
        } catch (Exception e) {
            return "ILM策略创建/更新失败: " + e.getMessage();
        }
    }
    
    /**
     * 创建索引模板并关联ILM策略
     * 
     * @param templateName 模板名称
     * @param indexPattern 索引模式
     * @param retentionMonths 保留月数
     * @return 操作结果
     */
    @PostMapping("/template")
    public String createIndexTemplate(
            @RequestParam(defaultValue = "audit-logs-template") String templateName,
            @RequestParam(defaultValue = "audit-logs-*") String indexPattern,
            @RequestParam(defaultValue = "6") int retentionMonths) {
        try {
            // 构建策略名称
            String policyName = "audit-log-policy-" + retentionMonths + "m";
            
            // 创建索引模板
            boolean success = elasticsearchILMService.createIndexTemplate(templateName, indexPattern, policyName);
            
            if (success) {
                return "索引模板创建成功，策略: " + policyName;
            } else {
                return "索引模板创建失败";
            }
        } catch (Exception e) {
            return "索引模板创建失败: " + e.getMessage();
        }
    }
    
    /**
     * 删除ILM策略
     * 
     * @param retentionMonths 保留月数
     * @return 操作结果
     */
    @DeleteMapping("/policy")
    public String deletePolicy(@RequestParam(defaultValue = "6") int retentionMonths) {
        try {
            // 构建策略名称
            String policyName = "audit-log-policy-" + retentionMonths + "m";
            
            // 删除ILM策略
            boolean success = elasticsearchILMService.deleteILMPolicy(policyName);
            
            if (success) {
                return "ILM策略删除成功，保留月数: " + retentionMonths;
            } else {
                return "ILM策略删除失败";
            }
        } catch (Exception e) {
            return "ILM策略删除失败: " + e.getMessage();
        }
    }
    
    /**
     * 创建初始索引
     * 
     * @param aliasName 别名名称
     * @return 操作结果
     */
    @PostMapping("/initial-index")
    public String createInitialIndex(@RequestParam(defaultValue = "audit-logs") String aliasName) {
        try {
            // 创建初始索引
            boolean success = elasticsearchILMService.createInitialIndexWithAlias(aliasName);
            
            if (success) {
                return "初始索引创建成功，别名: " + aliasName;
            } else {
                return "初始索引创建失败";
            }
        } catch (Exception e) {
            return "初始索引创建失败: " + e.getMessage();
        }
    }
    
    /**
     * 删除冲突的索引或数据流
     * 
     * @param name 索引或数据流名称
     * @return 操作结果
     */
    @DeleteMapping("/conflict")
    public String deleteConflictingIndexOrDataStream(@RequestParam String name) {
        try {
            // 删除冲突的索引或数据流
            boolean success = elasticsearchILMService.deleteConflictingIndexOrDataStream(name);
            
            if (success) {
                return "冲突的索引或数据流删除成功: " + name;
            } else {
                return "删除失败或不存在冲突";
            }
        } catch (Exception e) {
            return "删除冲突的索引或数据流失败: " + e.getMessage();
        }
    }
    
    /**
     * 获取所有ILM策略
     * 
     * @return 策略信息
     */
    @GetMapping("/policies")
    public String getAllILMPolicies() {
        try {
            String policies = elasticsearchILMService.getAllILMPolicies();
            return policies;
        } catch (Exception e) {
            return "获取ILM策略失败: " + e.getMessage();
        }
    }
}



